#include <REGX52.H>
#include "LCD1602.h"

void Delay20ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 36;
	j = 217;
	do
	{
		while (--j);
	} while (--i);
}

	
unsigned char Matrixkey()	
{
	unsigned char keynumber=0;
	P3&=0x03;		//0000 00xx
	P2|=0x1f;		//xxx1 1111
	while(1)
	{
		if(P2_4==0){keynumber=1;break;}
		if(P2_3==0){keynumber=2;break;}
		if(P2_2==0){keynumber=3;break;}
		if(P2_1==0){keynumber=4;break;}
		if(P2_0==0){keynumber=5;break;}
	}
	P3|=0xfc;		//1111 11xx
	P2&=0xe0;		//xxx0 0000
	Delay20ms();
	while(1)
	{
		if(P3_2==0){Delay20ms();while(P3_2==0);keynumber+=0;break;}
		if(P3_3==0){Delay20ms();while(P3_3==0);keynumber+=5;break;}
		if(P3_4==0){Delay20ms();while(P3_4==0);keynumber+=10;break;}
		if(P3_5==0){Delay20ms();while(P3_5==0);keynumber+=15;break;}
		if(P3_6==0){Delay20ms();while(P3_6==0);keynumber+=20;break;}
		if(P3_7==0){Delay20ms();while(P3_7==0);keynumber+=25;break;}
	}
	P3|=0xfc;		//1111 11xx
	P2|=0x1f;		//xxx1 1111	
	return keynumber;
}


	
	
	
	
	
	
	